<div class="modal-dialog" role="document">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title" id="exampleModalLabel">添加用户组</h4>
        </div>
        <form style="margin:4%;" id="addRoleForm" data-toggle="validator" role="form">
            <div class="form-group">
                <label for="name" class="control-label">用户组:</label>
                <input type="text" id="name" class="form-control" name="name" required>
            </div>
            <div class="form-group">
                <label for="introduce" class="control-label">描述:</label>
                <input type="text" id="introduce" class="form-control" name="introduce" required>
            </div>
            <div class="form-group">
                <label for="message-text" class="control-label">状&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;态:</label>
                <input type="checkbox" name="_status" value="1" />
                <input type="hidden" name="status" value="1" />
            </div>
            <div class="form-group">
                <label for="message-text" class="control-label">APP登陆:</label>
                <input type="checkbox" name="_app_login" value="0" />
                <input type="hidden" name="app_login" value="0" />
            </div>
            <div class="form-group">
                <label for="message-text" class="control-label">后台登陆:</label>
                <input type="checkbox" name="_back_login" value="1" />
                <input type="hidden" name="back_login" value="1" />
            </div>
        </form>
        <div class="modal-footer">
            <button type="button" data-dismiss="modal" class="btn btn-default">取消</button>
            <button type="button" id="roleSave" class="btn btn-primary">保存</button>
        </div>
    </div>
</div>


<script>
    $(function () {
        $('#addRoleForm').validator();
        var switchOption = {
            onText: "开",
            offText: "关",
            onColor: "success",
            offColor: "info",
            state: true,
            onSwitchChange: function (event, state) {
                if (state == true) {
                    $(this).val("1");
                } else {
                    $(this).val("0");
                }
                var value = $(this).val();
                $("input:hidden[name='"+$(this).attr("name").substring(1)+"']").val(value);
            }
        };
        $("input:checkbox[name='_status']").bootstrapSwitch(switchOption);

        $("input:checkbox[name='_back_login']").bootstrapSwitch(switchOption);
        switchOption.state = false;
        $("input:checkbox[name='_app_login']").bootstrapSwitch(switchOption);
    })

    $("#roleSave").click(function () {
        var validator = $("#addRoleForm").data("bs.validator");
        validator.validate();
        if (!validator.hasErrors()){
             //console.log(true);
             $.ajax({
                url: "{:U('Admin/Role/addRole')}",
                type: "post",
                dataType: "json",
                data: {
                    name: $("input[name='name']").val(),
                    introduce: $("input[name='introduce']").val(),
                    status: $("input[name='status']").val(),
                    app_login: $("input[name='app_login']").val(),
                    back_login: $("input[name='back_login']").val()
                },
                success: function (data) {
                    if (data.status) {
                        $("#addRole").modal('toggle');
                        window.location.reload();
                    } else {
                        common.alert("提示",data.msg);
                    }
                }
            })
        }           
    })
</script>